package gsar.client;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Set;

import javax.swing.JFrame;

import manualClientInterface.*;



/*
Copyright (c) 2009, Eric Eaton, University of Maryland Baltimore County
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the University of Maryland Baltimore County nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/** A sample manual search and rescue client.
 *
 * DO NOT MODIFY THE CONTENTS OF THIS FILE -- it may be overwritten in
 * later releases of the Search and Rescue simulator.
 *
 * @author Eric Eaton
 */
public class MyAgent extends gsar.client.Client {
	
	//-----------------------------------
	// Constantes
	//----------------------------------
	
	public final int SIZE = 50;
	
	//-----------------------------------
	// Atributos
	//----------------------------------
	
	JFrame frame = new JFrame("Manual Agent");
    Consola console = new Consola(frame);
    
    
    GWObject[][] memory = new GWObject[50][50];
    
	
	public static void main(String[] args) {
		
	
		main(new MyAgent(), args);
	    
	    
	 }

	
	@Override
	protected GWAction chooseAction(Perceptions perceptions) {
		
	    
		GWAction chosenAction;
		
		
		// display the current perceptions
		GWMap mExits = getMap();
	
		Set<GWCoordinate> lista = perceptions.listPerceivedCoordinates();
		
		for (GWCoordinate gwCoor : lista) {
			
			GWCell cell = perceptions.getPerceivedCell(gwCoor);
			GWObject objeto = cell.getOccupant();
			
			memory[gwCoor.x][gwCoor.y] = objeto;
			
		}
		
		
		console.imprimirSalida("Waiting for the server to send perceptions...");
		console.imprimirSalida("Current time:  "+getCurrentTime());
		console.imprimirSalida("Perceptions:  ");
		console.imprimirSalida(perceptions.toString());
	
		
		console.imprimirSalida("Perceived locations:  "+perceptions.listPerceivedCoordinates());
		console.imprimirSalida("Perceived objects:  "+perceptions.listPerceivedObjects());
		console.imprimirSalida("****Exits:  " + mExits.getMarkedCoordinates("EXIT"));
	
		
		console.imprimirSalida("What is your next action?");
		console.imprimirSalida("  (-)NoOp");
		console.imprimirSalida("Moves:   (N)orth  (E)ast    (S)outh    (W)est");
		console.imprimirSalida("Actions: (P)ickup  (D)ropoff  (C)Sense  (-)NoOp");
		console.imprimirSalida("     Followed by:   (N)orth  (E)ast    (S)outh    (W)est");
	
		
		console.imprimirSalida("MEMORIA");
		
		for (int i = 0; i < memory.length; i++) {
			for (int j = 0; j < memory.length; j++) {
				
				if(memory[i][j]!= null)
				console.imprimirSalida("( " + i + ", " + j + " ) OBJECT " + memory[i][j].toString() );
				
			}
		}
		
		chosenAction = console.getInput();
		
				// print out the chosen action and return it
		console.imprimirSalida("Choosing action:  "+chosenAction);
		
		return chosenAction;
	}


	@Override
	protected void initialize() {
		console.startInput();
	    console.imprimirSalida("Waiting for the simulation to start...");
	}


	@Override
	protected void processResult(double score) {
		
		
		
		console.imprimirSalida("Final score:  "+score+" rescued victims.");
		console.imprimirSalida("Goodbye.");
	}

}
